import {toast} from "../../../utils/tool";

const app = getApp()
Component({
    properties: {},
    data: {
        showPop: false,
        mealList: [],
        active: 0,
        roomName: '',
        roomId: '',
        code: ''
    },
    methods: {
        confirmHandle() {
            const {code, roomId, mealList, active} = this.data
            const target = mealList[active]
            if (!target) return toast({title: "请选择套餐"})
            app.ajax({
                url: '/api/user/yanquanOrder',
                method: 'POST',
                data: {
                    qr_code: code,
                    room_id: roomId,
                    taocan_id: target.id
                },
                success: resp => {
                    const {code, msg} = resp
                    if (code !== 1) return toast({title: msg})
                    this.onClose()
                    toast({title: msg, time: 1500}, () => {
                        wx.switchTab({
                            url: '/pages/my-order/my-order'
                        })
                    })
                }
            })
        },
        onClose() {
            this.setData({
                showPop: false,
            })
            setTimeout(() => {
                this.reset()
            }, 300)
        },
        changeMeal(ev) {
            const {index} = ev.currentTarget.dataset
            if (this.data.activeMeal === index) return
            this.setData({
                active: index
            })
        },
        reset() {
            this.setData({
                mealList: [],
                active: 0,
                roomName: '',
                roomId: '',
                code: ''
            })
        },
        open(data) {
            const {taocan, name, id, qr_code,} = data
            this.setData({
                roomId: id,
                code: qr_code,
                roomName: name,
                mealList: taocan.map(item => {
                    const {taocan_price, name, id, time} = item
                    return {
                        price: taocan_price, name, id, time
                    }
                })
            })
            this.setData({
                showPop: true
            })
        }
    }
});
